//
//  ShenQingViewController.swift
//  demo
//
//  Created by toro宇 on 2020/2/17.
//  Copyright © 2020 CodeYu. All rights reserved.
//

import UIKit

let TFHeight = 45

class ShenQingViewController: UIViewController {
    
    
    //MARK: 短信验证码
    var countdownTimer: Timer? // timer定时器
    // 创建属性观察器，属性改变之前调用(倒计时的值)
    var remainingSeconds: Int = 0{
        willSet{
            //在按钮的普通状态下改变 按钮的标题(.normal:普通状态; .highlighted:触摸状态下的文字; .disabled:禁用状态下的文字)
            codeBtn.setTitle("重发(\(newValue))s", for: .normal)
            if newValue <= 0  {
                codeBtn.setTitle("获取验证码", for: .normal)
                isCounting = false
            }
        }
    }
    //属性观察器,属性改变之前调用(该属性用于标识计时是结束还是开始;true:为开始计时;false:为结束计时)
    var isCounting = false{
        willSet{
            if newValue {
                // 开始计时
                countdownTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { (timer) in
                    self.remainingSeconds -= 1
                })
                // 初始化倒计时的值
                remainingSeconds = 60;
                //开启计时器设置按钮的背景颜色
                codeBtn.backgroundColor = UIColor.gray
            }else{
                // 结束倒计时
                countdownTimer?.invalidate()// 关闭计时
                countdownTimer = nil //将计时器Timer的执行函数滞空
                codeBtn.backgroundColor = UIColor(hex: "ff4b52")//按钮背景色设置为红色

            }
            //设置按钮是否可点击(当计时开始的时候按钮不可点击,计时结束的时候按钮可以点击,所以取的是newValue的反值)
            codeBtn.isEnabled = !newValue

        }
    }
    
    
    
    // MARK: -UIProperty
    
    // 导航栏
    lazy var nav = HZCustomNavigationBar.customNavigationBar().then { (bar) in
        bar.barBackgroundColor = UIColor(hex: "fbfbfb")
        bar.shadowImageHidden = false
        /* 闭包 相当于block
         { (参数) -> (返回值) in
         
         }
         */
        bar.hz.setItemsToLeft([HZNavigationBarItem.create(normalImage: "back", barItemClickHandler: { (item) in
            self.navigationController?.popViewController(animated: true)
        }), HZNavigationBarItem.create(normalTitle: "广发银行ETC卡申请", barItemClickHandler: {
            (item) in
        })])
    }
    
    // 标题
    let biaoshiLab = UILabel().then {
        $0.text = "请填写邮寄地址"
        $0.textColor = UIColor(hex: "ff8e99")
        $0.font = UIFont.systemFont(ofSize: 14)
    }
//
    //内容
    var addressOneTF = UITextField().then({
        $0.textColor = UIColor.black
        let attr = NSAttributedString.init(string: "请输入地址", attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 13)])
        $0.attributedPlaceholder = attr

    })
   lazy var addressBtn = UIButton().then{
        $0.setImage(UIImage(named:"address"), for: .normal)
        $0.rx.tap.subscribe(onNext:{ [weak self] in
            print("选择地址")
        })
    }
    var addressTwoTF = UITextField().then({
        $0.textColor = UIColor.black
        let attr = NSAttributedString.init(string: "请输入详细地址", attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 13)])
        $0.attributedPlaceholder = attr


    })
    var phoneTF = UITextField().then({
        $0.textColor = UIColor(hex: "bcbcbc")
        $0.textAlignment = NSTextAlignment.right
        let attr = NSAttributedString.init(string: "请输入手机号", attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 13)])
             $0.attributedPlaceholder = attr

    })
    
    lazy var codeBtn = UIButton(type: .custom).then{
        $0.backgroundColor = UIColor(hex: "ff4b52")
        $0.layer.cornerRadius = 6
        $0.layer.masksToBounds = true
        $0.setTitle("获取验证码", for: .normal)
        $0.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        $0.setTitleColor(UIColor.white, for: .normal)
        $0.rx.tap.subscribe(onNext:{ [weak self] in
            self?.isCounting = true
            })
        //设置计时开始
        
    }
    
    var codeTF = UITextField().then({
        $0.textColor = UIColor(hex: "bcbcbc")
        $0.textAlignment = NSTextAlignment.right
        let attr = NSAttributedString.init(string: "请输入短信验证码", attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 13)])
             $0.attributedPlaceholder = attr

        
    })
    
    lazy var contentView = UIView().then { (contentView) in
        contentView.backgroundColor = UIColor.white
        // 邮寄地址
        let youjiTitleLab = UILabel().then{
            var str : NSMutableAttributedString = NSMutableAttributedString.init(attributedString: NSAttributedString(string: "* 邮寄地址"))
            str.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor(hex: "ff8e99"), range: NSRange.init(location: 0, length: 1))
            $0.font = UIFont.systemFont(ofSize: 15)
            $0.textColor = UIColor.black
            $0.attributedText = str
        }
        contentView.addSubview(youjiTitleLab)
        youjiTitleLab.snp.makeConstraints({ (make) in
            make.left.equalToSuperview().offset(15)
            make.top.equalToSuperview().offset(20)
        })
        
        contentView.addSubview(addressOneTF)
        contentView.addSubview(addressBtn)

        addressOneTF.snp.makeConstraints({ (make) in
            make.left.equalToSuperview().offset(25)
            make.top.equalTo(youjiTitleLab.snp.bottom).offset(1)
            make.height.equalTo(TFHeight)
            make.right.equalTo(addressBtn.snp.left).offset(-15)
        })
        
        addressBtn.snp.makeConstraints({ (make) in
            make.right.equalToSuperview().offset(-15)
            make.centerY.equalTo(addressOneTF)
            make.width.equalTo(20)
            
        })
        
        let addressOneLine = UIView().then{
            $0.backgroundColor = UIColor(hex:"cacaca")
        }
        contentView.addSubview(addressOneLine)
        addressOneLine.snp.makeConstraints({ (make) in
            make.left.right.equalToSuperview();
            make.top.equalTo(addressOneTF.snp.bottom)
            make.height.equalTo(0.5)
        })
        
        contentView.addSubview(addressTwoTF)
        addressTwoTF.snp.makeConstraints({ (make) in
            make.left.right.equalTo(addressOneTF)
            make.height.equalTo(TFHeight)
            make.top.equalTo(addressOneLine.snp.bottom)
        })

        let addressTwoLine = UIView().then{
            $0.backgroundColor = UIColor(hex:"cacaca")
        }
        contentView.addSubview(addressTwoLine)
        addressTwoLine.snp.makeConstraints({ (make) in
            make.left.right.equalToSuperview();
            make.top.equalTo(addressTwoTF.snp.bottom)
            make.height.equalTo(0.5)
        })
        

        /**
         手机号
         */
        let phoneTitleLab = UILabel().then{
            var str : NSMutableAttributedString = NSMutableAttributedString.init(attributedString: NSAttributedString(string: "* 手机号码"))
            str.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor(hex: "ff8e99"), range: NSRange.init(location: 0, length: 1))
            $0.font = UIFont.systemFont(ofSize: 15)
            $0.textColor = UIColor.black
            $0.attributedText = str
        }

        contentView.addSubview(phoneTitleLab)
        contentView.addSubview(phoneTF)
        contentView.addSubview(codeBtn)

        phoneTF.snp.makeConstraints({ (make) in
        make.left.equalTo(phoneTitleLab.snp_rightMargin).offset(25)
            make.top.equalTo(addressTwoLine.snp.bottom)
            make.right.equalTo(codeBtn.snp.left).offset(-10)
            make.width.greaterThanOrEqualTo(40)
            make.height.equalTo(TFHeight)

        })

        phoneTitleLab.snp.makeConstraints({ (make) in
            make.left.equalToSuperview().offset(15)
            make.centerY.equalTo(phoneTF)
        })


        codeBtn.snp.makeConstraints({ (make) in
            make.right.equalTo(addressBtn)
            make.centerY.equalTo(phoneTF)
            make.size.equalTo(CGSize.init(width: 90, height: 28))

        })

        let phoneLine = UIView().then{
            $0.backgroundColor = UIColor(hex:"cacaca")
        }
        contentView.addSubview(phoneLine)
        phoneLine.snp.makeConstraints({ (make) in
            make.left.right.equalToSuperview();
            make.top.equalTo(phoneTF.snp.bottom)
            make.height.equalTo(0.5)
        })


        /**
         验证码
         */
        let codeTitleLab = UILabel().then{
            var str : NSMutableAttributedString = NSMutableAttributedString.init(attributedString: NSAttributedString(string: "* 短信验证码"))
            str.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor(hex: "ff8e99"), range: NSRange.init(location: 0, length: 1))
            $0.font = UIFont.systemFont(ofSize: 15)
            $0.textColor = UIColor.black
            $0.attributedText = str
        }
        contentView.addSubview(codeTitleLab)
        contentView.addSubview(codeTF)

        codeTitleLab.snp.makeConstraints({ (make) in
            make.left.equalTo(youjiTitleLab)
            make.centerY.equalTo(codeTF)
        })

        codeTF.snp.makeConstraints({ (make) in
        make.left.equalTo(codeTitleLab.snp.right).offset(25)
            make.right.equalTo(addressBtn)
            make.top.equalTo(phoneLine.snp.bottom)
            make.height.equalTo(TFHeight)
            make.bottom.equalToSuperview()
        })
    }
    
    var sendBtn = UIButton().then{
        $0.backgroundColor = UIColor(hex: "ff4b52")
        $0.layer.cornerRadius = 3
        $0.layer.masksToBounds = true
        $0.setTitle("提交", for: .normal)
        $0.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        $0.setTitleColor(UIColor.white, for: .normal)
    }
    
    
    // MARK: VieweDid

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor(hex: "f4f4f4")
        view.addSubview(nav)
        
        view.addSubview(biaoshiLab)
        biaoshiLab.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(nav.snp.bottom).offset(15)
        }
    
        view.addSubview(contentView)
        contentView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(biaoshiLab.snp.bottom).offset(15)
        }
        
        view.addSubview(sendBtn)
        sendBtn.snp.makeConstraints { (make) in
            make.top.equalTo(contentView.snp.bottom).offset(50)
                make.left.equalToSuperview().offset(15)
                make.right.equalToSuperview().offset(-15)
                make.height.equalTo(50)
        }
        
    }


}


